<template>
    
    <div id="roleModify">
        <div>
            <el-form ref="submitForm" :model="submitForm" :rule="rules" class="formEmbedTable mt20">
                <table>
					<caption>Modify</caption>
                    <tr>
                        <td>Role No.</td>
                        <td>{{submitForm.roleNo}}</td>
                        <td>Role Category</td>
                        <td >{{"Branch"}}</td>			
                    </tr>
                    <tr>
                        <td>Role Name</td>
                        <td >
                            <el-form-item prop="roleName">
                                <el-input v-model="submitForm.roleName"></el-input>
                            </el-form-item>
                        </td>
                        <td>Describtion</td>
                        <td >
                            <el-form-item prop="roleDesc">
                                <el-input v-model="submitForm.roleDesc" autosize></el-input>
                            </el-form-item>
                        </td>					
                    </tr>
                </table>
            </el-form>  
        </div>
        <!--<div class="mt20">
			<ynet-tree :menuTree="menuTree" @modifyMenuTree="modifyMenuTree"></ynet-tree>
		</div>-->
        <div class="mt20">
			<RoleTree :menuTree="menuTree" @modifyMenuTree="modifyMenuTree"></RoleTree>
		</div>
        <div class="btnArea mt20 tac">
            <el-button type="primary" @click="queryAuth('submitForm')">Confirm</el-button>
            <!--<el-button type="primary" @click="resetForm('submitForm')">重置</el-button>-->
            <el-button type="primary" @click="backMenu()">Cancel</el-button>
        </div>
        <!--授权确认页面弹窗-->
        <div>
            <ynet-authDialog ref="dialogTable"  @getAuthForm="getAuthForm"></ynet-authDialog>
        </div>
    </div>
</template>
<script>
     import ghbIcon from '@src/components/icon'
     import RoleTree from './editTree'
    export default {
        name:'roleModify',
        components:{
            ghbIcon,
            RoleTree,
        },
        props:['editForm','roleBsnDetail',],
        mounted(){
            this.submitForm=$.extend({},this.submitForm,this.editForm);
            this.roleBsnList=[];//出示菜单值
            
            this.menuTree=this.$tools.storage.get("menuCopy", "save")//从缓存中获取操作员菜单树
            //console.log("menuTree;"+JSON.stringify(this.menuTree))
            //this.menuTree.splice(0,1);//去掉Home菜单
            //console.log("menuTree:"+JSON.stringify(this.menuTree))
            // this.roleBsnList=this.roleBsnDetail;//保存初始值  这样只赋值了引用，而不是数组对象，roleBsnList会随着roleBsnDetail改变而改变
            //用以下循环单独创建roleBsnList,这样就不会随roleBsnDetail改变而改变
            for(let item of this.roleBsnDetail){
                this.roleBsnList.push(item)
            }
            //console.log("roleBsnDetail:"+JSON.stringify(this.roleBsnDetail))
            // 给菜单权限赋值 反显用户菜单权限
            //第一个节点
            
            for(let i=0;i<this.menuTree.length;i++){
                for(let j=0;j<this.roleBsnDetail.length;j++){
                    if(this.menuTree[i].bsnCode==this.roleBsnDetail[j].bsnCode){
                        if(this.roleBsnDetail[j].menuFlag==1 && this.roleBsnDetail[j].menuAuth==1){//有操作权限
                            this.menuTree[i].menuTreeOption=true;
                            this.menuTree[i].menuTreeVerify=true;
                            this.roleBsnDetail.splice(j,1)
                        }else if(this.roleBsnDetail[j].menuAuth==1 && this.roleBsnDetail[j].menuFlag==0){//授权权限
                            this.menuTree[i].menuTreeVerify=true;
                            this.roleBsnDetail.splice(j,1)
                        }else if(this.roleBsnDetail[j].menuAuth==0 && this.roleBsnDetail[j].menuFlag==1){
                            this.menuTree[i].menuTreeOption=true;
                            this.roleBsnDetail.splice(j,1)
                        }
                    }
                }
                //循环第二个节点 先判断是否有第二个节点
                if(this.menuTree[i].childrens){
                    for(let ii=0;ii<this.menuTree[i].childrens.length;ii++){
                        for(let j=0;j<this.roleBsnDetail.length;j++){
                            if(this.menuTree[i].childrens[ii].bsnCode==this.roleBsnDetail[j].bsnCode){
                                if(this.roleBsnDetail[j].menuFlag==1 && this.roleBsnDetail[j].menuAuth==1){//有操作权限
                                    this.menuTree[i].childrens[ii].menuTreeOption=true;
                                    this.menuTree[i].childrens[ii].menuTreeVerify=true;
                                    this.roleBsnDetail.splice(j,1)
                                }else if(this.roleBsnDetail[j].menuFlag==0 && this.roleBsnDetail[j].menuAuth==1){//授权权限
                                    this.menuTree[i].childrens[ii].menuTreeVerify=true;
                                    this.roleBsnDetail.splice(j,1)
                                }else if(this.roleBsnDetail[j].menuFlag==1 && this.roleBsnDetail[j].menuAuth==0){
                                    this.menuTree[i].childrens[ii].menuTreeOption=true;
                                    this.roleBsnDetail.splice(j,1)
                                }
                            }
                        }
                        //循环第三个节点 先判断是否有第三个节点
                        if(this.menuTree[i].childrens[ii].childrens){//有第三个节点
                            for(let iii=0;iii<this.menuTree[i].childrens[ii].childrens.length;iii++){
                                for(let j=0;j<this.roleBsnDetail.length;j++){
                                    if(this.menuTree[i].childrens[ii].childrens[iii].bsnCode==this.roleBsnDetail[j].bsnCode){
                                        if(this.roleBsnDetail[j].menuFlag==1 && this.roleBsnDetail[j].menuAuth==1){//有操作权限
                                            this.menuTree[i].childrens[ii].childrens[iii].menuTreeOption=true;
                                            this.menuTree[i].childrens[ii].childrens[iii].menuTreeVerify=true;
                                            this.roleBsnDetail.splice(j,1)
                                        }else if(this.roleBsnDetail[j].menuFlag==0 && this.roleBsnDetail[j].menuAuth==1){//授权权限
                                            this.menuTree[i].childrens[ii].childrens[iii].menuTreeVerify=true;
                                            this.roleBsnDetail.splice(j,1)
                                        }else if(this.roleBsnDetail[j].menuFlag==1 && this.roleBsnDetail[j].menuAuth==0){
                                            this.menuTree[i].childrens[ii].childrens[iii].menuTreeOption=true;
                                            this.roleBsnDetail.splice(j,1)
                                        }
                                        
                                    }
                                }
                            }
                        }
                    }
                }
            };
            console.log("menuTree;"+JSON.stringify(this.menuTree))
        },
        data(){
            return{
                needAuth:'',
                authType:'',  
                menuTree:[],//传给数组件的树结构
                dialogTableVisible:false,
                submitForm:{
                    roleNo:'',roleName:'',roleType:'',
                    roleDesc:'',imBsnList:[],
                    actionType:'1',bsnCode:'IM0102',
                },
                imBsnList:[],//从树组件传回来的值
                authForm: { authUserNo: '', password: '',randomField:''},
                rules:{
                    roleName:[{required:true,}],
                },
                roleBsnList:[],//角色已有的菜单
                imBsnListTemp:[],//从树结构传回来的临时数据
            }
        },
        methods:{
            getAuthForm(val){
                //获取授权数据
                this.authForm=val;
                // console.log("val:"+JSON.stringify(val));
                this.submitForm=$.extend(this.submitForm,this.authForm);
                this.submit()
            },
            queryAuth(formName){//查询授权模式
                this.$refs[formName].validate((valid)=>{
                    if(valid){
                        let body={};body.bsnCode="IM0102";body.version="IM0102",
                        this.$tools.request(this,"IM010001Action.do",body).then(data=>{
                            if(data.body.errorCode==0){
                                if(data.body.needAuth==0){//无需授权
                                    this.submit()
                                }else if(data.body.needAuth==1 && data.body.authType==1){
                                    this.needAuth=1;
                                    this.authType=1;
                                    this.$refs["dialogTable"].show()
                                    // this.dialogTableVisible=true;
                                }else if( data.body.needAuth==1 && data.body.authType==2 ){
                                    this.needAuth=1;
                                    this.authType=2;
                                    this.submit()
                                }else{
                                    this.$alert('Authorization type error', 'Warning', {
                                        confirmButtonText: 'Confirm',
                                        callback: action => {
                                        }
                                    });
                                }
                            }else{
                                this.$alert("System Error Try again","Warning",{
                                    confirmButtonText: 'Confirm',
                                    callback: action => {
                                    }
                                })
                            }
                            
                        })
                    }
                })
                
			},
            backMenu(){
                this.$emit('backMenu');
            },
            modifyMenuTree(val){
                this.imBsnList=val ;//从树组件获取的数据
                //console.log("val:"+JSON.stringify(val))
            },
            submit(){
                //封装父菜单
                this.imBsnListTemp=this.imBsnList;
                let parentTemp = []
                //二级菜单 start
                for(let item1 of this.menuTree){
                    for(let item11 of item1.childrens){
                        if(item11.bsnCode.length==6){
                            let isParentShow = 0  //0没有修改 1修改成menuFlag=0 2修改成menuFlag=1
                            for(let item2 of this.imBsnListTemp){
                                if(item2.bsnCode.length==8 && item11.bsnCode==item2.bsnCode.substring(0,6)){
                                    if(item2.menuFlag=='1'){
                                        isParentShow = 2
                                        break
                                    }
                                    isParentShow = 1
                                }
                            }
                            //查询该角色下的二级菜单下是否还有其他未勾选的菜单，若有判断menuFlag是否等于1，若是，isParentShow=2
                            if(isParentShow==1){
                                let roleBsnItems = []
                                for(let item2 of this.imBsnListTemp){
                                    for(let rBsnItem of this.roleBsnList){
                                        if(rBsnItem.bsnCode.length!=8 || rBsnItem.bsnCode==item2.bsnCode ||rBsnItem.bsnCode.substring(0,6)!=item2.bsnCode.substring(0,6)) continue
                                        let hasEM = false
                                        for(let item of roleBsnItems){
                                            if(rBsnItem.bsnCode==item.bsnCode) hasEM = true
                                        }
                                        if(!hasEM) roleBsnItems.push(rBsnItem)
                                    }
                                }
                                for(let item2 of this.imBsnListTemp){
                                    for(let index in roleBsnItems){
                                        if(item2.bsnCode==roleBsnItems[index].bsnCode) roleBsnItems.splice(index,1)
                                    }
                                }
                                for(let item of roleBsnItems){
                                    if(item.menuFlag=='1'){
                                        isParentShow = 2
                                        break
                                    }
                                    isParentShow = 1
                                }
                            }
                            if(isParentShow!=0){
                                parentTemp.push({bsnCode:item11.bsnCode,bsnName:'',menuFlag:isParentShow==2?'1':(isParentShow==1?'0':item11.menuFlag),menuAuth:'1'})
                            }
                        }
                    }
                }
                //去掉重复的
                for(let item1 of this.roleBsnList){
                    for(let j=0;j<parentTemp.length;j++){
                        let item2 = parentTemp[j]
                        if(item1.bsnCode==item2.bsnCode && item1.menuFlag==item2.menuFlag && item1.menuAuth==item2.menuAuth){
                            parentTemp.splice(j,1)
                        }
                    }
                }
                for(let item of parentTemp){
                    this.imBsnListTemp.push(item)
                }
                //二级菜单 end
                //一级菜单 start
                parentTemp = []
                for(let item1 of this.menuTree){
                    if(item1.bsnCode.length==4){  //一级菜单
                        let isParentShow = 0
                        for(let item2 of this.imBsnListTemp){
                            if(item2.bsnCode.length==6 && item1.bsnCode==item2.bsnCode.substring(0,4)){
                                if(item2.menuFlag=='1'){
                                    isParentShow = 2
                                    break
                                }
                                isParentShow = 1
                            }
                        }
                        //查询该角色下的一级菜单下是否还有其他未勾选的菜单，若有判断menuFlag是否等于1，若是，isParentShow=2
                        if(isParentShow==1){
                            let roleBsnItems = []
                            for(let item2 of this.imBsnListTemp){
                                for(let rBsnItem of this.roleBsnList){
                                    if(rBsnItem.bsnCode.length!=6 || rBsnItem.bsnCode==item2.bsnCode ||rBsnItem.bsnCode.substring(0,4)!=item2.bsnCode.substring(0,4)) continue
                                    let hasEM = false
                                    for(let item of roleBsnItems){
                                        if(rBsnItem.bsnCode==item.bsnCode) hasEM = true
                                    }
                                    if(!hasEM) roleBsnItems.push(rBsnItem)
                                }
                            }
                            for(let item2 of this.imBsnListTemp){
                                for(let index in roleBsnItems){
                                    if(item2.bsnCode==roleBsnItems[index].bsnCode) roleBsnItems.splice(index,1)
                                }
                            }
                            for(let item of roleBsnItems){
                                if(item.menuFlag=='1'){
                                    isParentShow = 2
                                    break
                                }
                                isParentShow = 1
                            }
                        }
                        if(isParentShow!=0){
                            parentTemp.push({bsnCode:item1.bsnCode,bsnName:'',menuFlag:isParentShow==2?'1':(isParentShow==1?'0':item1.menuFlag),menuAuth:'1'})
                        }
                    }
                }
                //去掉重复的
                for(let item1 of this.roleBsnList){
                    for(let j=0;j<parentTemp.length;j++){
                        let item2 = parentTemp[j]
                        if(item1.bsnCode==item2.bsnCode && item1.menuFlag==item2.menuFlag && item1.menuAuth==item2.menuAuth){
                            parentTemp.splice(j,1)
                        }
                    }
                }
                for(let item of parentTemp){
                    this.imBsnListTemp.push(item)
                }
                //一级菜单 end
                this.submitForm.imBsnList=this.imBsnListTemp

                this.$tools.request(this,"IM010202Action.do",this.submitForm).then(data=>{
                    if(data.body.errorCode==0){
                        if(this.needAuth==1 && this.authType==2){
                            this.$message.success('Waiting for Authorized')
                            this.$emit('backMenu');
                        }else if(this.needAuth==1 && this.authType==1){
                            if(data.body.authStatus==1){
                                this.$message.error({
                                     message:'Authorship No. or password wrong ,please resume load',
                                    showClose:true,
                                })
                            }else{
                                this.$message.success('Modify Success')
                                this.$emit('backMenu');
                            }
                        }else{
                            this.$message.success('Modify Success')
                            this.$emit('backMenu');
                        }
                        
                    }else{
                        this.$alert("System Error Try again", "warning",{
                            confirmButtonText: 'Confirm',
                            callback: action => {
                            }
                        })
                    }
                })
            },
            resetForm(formName){
				this.$refs[formName].resetFields();
			},
        }
    }

</script>
